package com.zxk.crawler.controller;

import com.zxk.core.common.util.R;
import com.zxk.crawler.service.EsDataHandleService;
import org.elasticsearch.action.search.SearchResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author zxk
 * @DATE 2022/7/27 15:06
 * @Version 1.0
 **/
@RestController
@RequestMapping("/blog/crawler")
public class BlogCrawlerNewsController {
    @Resource
    private EsDataHandleService esDataHandleService;

    @PostMapping("/search/news")
    public R  searchNewsByCondition(@RequestBody Map<String,Object> condition) {
        String date = condition.get("date")+"";
        String[] fields = (String[]) condition.get("fields");
        SearchResponse response;
        if(null != fields) {
            response = esDataHandleService.simpleSearchNews(date, fields);
        } else {
            response = esDataHandleService.simpleSearchNews(date);
        }
        List<Map<String, Object>> mapList = esDataHandleService.handleSearchResult(response);
        return R.ok().setData(mapList);
    }

    @GetMapping("/news/test")
    public R newsTest() {
        return R.ok("没时间解释了，赶紧上车");
    }
}
